<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>会员卡管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/app/admin/component/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" media="all">
</head>
<body>
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">卡号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="card_no" placeholder="请输入会员卡号" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-inline">
                                <select name="status">
                                    <option value="">全部状态</option>
                                    <option value="0">未使用</option>
                                    <option value="1">已使用</option>
                                    <option value="2">已作废</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-card-search">
                                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                            </button>
                        </div>
                    </div>
                </div>

                <div style="padding-bottom: 10px;">
                    <button class="layui-btn layuiadmin-btn-list" data-type="batchVoid">批量作废</button>
                    <button class="layui-btn layuiadmin-btn-list" data-type="export">导出</button>
                </div>

                <table id="LAY-card-list" lay-filter="LAY-card-list"></table>
            </div>
        </div>
    </div>

    <script type="text/html" id="table-card-status">
        {{#  if(d.status === 0){ }}
            <span class="layui-badge layui-bg-blue">未使用</span>
        {{#  } else if(d.status === 1){ }}
            <span class="layui-badge layui-bg-green">已使用</span>
        {{#  } else if(d.status === 2){ }}
            <span class="layui-badge">已作废</span>
        {{#  } }}
    </script>

    <script type="text/html" id="table-card-operation">
        {{#  if(d.status === 0){ }}
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="void"><i class="layui-icon layui-icon-delete"></i>作废</a>
        {{#  } }}
    </script>

    <script src="/app/admin/component/layui/layui.js"></script>
    <script src="/app/admin/component/pear/pear.js"></script>
    <script>
    layui.use(['table', 'form', 'layer'], function(){
        var $ = layui.$
        ,table = layui.table
        ,form = layui.form
        ,layer = layui.layer;
        
        //会员卡列表
        var tableIns = table.render({
            elem: '#LAY-card-list'
            ,url: '/app/ky_shouyin/member/cardList'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'id', title: 'ID', width: 80, sort: true}
                ,{field: 'card_no', title: '卡号', width: 120}
                ,{field: 'member_name', title: '使用会员', width: 100}
                ,{field: 'status', title: '状态', width: 100, templet: '#table-card-status'}
                ,{field: 'created_at', title: '生成时间', width: 160}
                ,{title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-card-operation'}
            ]]
            ,page: true
            ,limit: 15
            ,text: {
                none: '暂无数据'
            }
        });
        
        //监听搜索
        form.on('submit(LAY-card-search)', function(data){
            var field = data.field;
            
            //执行重载
            table.reload('LAY-card-list', {
                where: field
            });
        });
        
        //监听工具条
        table.on('tool(LAY-card-list)', function(obj){
            var data = obj.data;
            if(obj.event === 'void'){
                layer.confirm('确定要作废该会员卡吗？', function(index){
                    $.ajax({
                        url: '/app/ky_shouyin/member/voidCard'
                        ,type: 'POST'
                        ,data: {id: data.id}
                        ,success: function(res){
                            if(res.code === 0){
                                layer.msg('作废成功', {icon: 1});
                                obj.update({
                                    status: 2
                                });
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }
                    });
                    layer.close(index);
                });
            }
        });
        
        var active = {
            batchVoid: function(){
                var checkStatus = table.checkStatus('LAY-card-list')
                ,checkData = checkStatus.data;
                
                if(checkData.length === 0){
                    return layer.msg('请选择要作废的会员卡');
                }
                
                layer.confirm('确定要作废选中的会员卡吗？', function(index){
                    var ids = checkData.map(function(item){
                        return item.id;
                    });
                    
                    $.ajax({
                        url: '/app/ky_shouyin/member/batchVoidCards'
                        ,type: 'POST'
                        ,data: {ids: ids}
                        ,success: function(res){
                            if(res.code === 0){
                                layer.msg('批量作废成功', {icon: 1});
                                tableIns.reload();
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }
                    });
                    layer.close(index);
                });
            }
            ,export: function(){
                var checkStatus = table.checkStatus('LAY-card-list')
                ,checkData = checkStatus.data;
                
                var ids = checkData.map(function(item){
                    return item.id;
                });
                
                location.href = '/app/ky_shouyin/member/exportCards?ids=' + ids.join(',');
            }
        };
        
        $('.layui-btn.layuiadmin-btn-list').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
    </script>
</body>
</html> 